package com.ysd.twocrm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.Student;
import com.ysd.twocrm.entity.StudentClass;
import com.ysd.twocrm.mapper.StudentClassMapper;
import com.ysd.twocrm.mapper.StudentMapper;
import com.ysd.twocrm.mapper.UserRolesMapper;
import com.ysd.twocrm.mapper.UsersMapper;
import com.ysd.twocrm.service.StudentClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/*
*@author:liusupeng
*@Date:2020/4/7 21:53
*@Description: 查询班级信息
*/
@RestController
@RequestMapping("/studentClass")
public class StudentClassController {
    @Autowired
    private StudentClassMapper studentClassMapper;
    @Autowired
    private StudentClassService studentClassService;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UserRolesMapper userRolesMapper;


    /*
     * @Description 查询全部系别
     * @param
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/8 10:56
     */
    @GetMapping
     public Object getAllDepartmen(){

         return studentClassMapper.getAllDepartment();
     }

    /*
    * getAllDepartment:根据系来查询当前系下的全部专业
    * @param : department:系别
    *
    * */

    @GetMapping("/1")
    public Object getAllSpecialty(String department){
        return studentClassMapper.getAllSpecialty(department);
    }
    /*
    * getAllSpecialty：根据专业来查询全部班级
    * @param
    * Specialty:专业
    * */
    @GetMapping("/2")
    public Object getAllClass(String Specialty)
     {
      //   System.out.println(">====="+studentClassMapper.getAllStuClass(Specialty));
        return studentClassMapper.getAllStuClass(Specialty);
    }

    /*
     * @Description 查询班级表内全部的字段
     * @param
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/8 13:57
     */
    @GetMapping("/3")
    public Object getAllStuClass(){


        return studentClassMapper.selectList(null);
    }
    /*
     * @Description 多条件分页查询班级  1：根据班级名称 2：系别 3：辅导员 4：教员
     * @param page 分页页码
     * @param s 学生班级参数
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/9 16:26
     */
    @GetMapping("/4")
    public Object findAllStuClass(Integer page,StudentClass s){
        return studentClassService.STUDENT_CLASS_LAYUI_UTILS(page,s);
    }
    /*
     * @Description判断班级名称是否存在
     * @param stuClass 班级名
     * @return java.lang.String
     * @author liusupeng
     * @date 2020/4/9 22:26
     */
@GetMapping("/5")
    public String findIfStuClass(String stuClass){

    StudentClass ifStudentClass = studentClassMapper.findIfStudentClass(stuClass);
    if(ifStudentClass!=null){
        return "success";
    }
    return "false";
    }
    /*
     * @Description 添加班级，并且添加系和专业
     * @param studentClass :全部班级信息
     * @return java.lang.String
     * @author liusupeng
     * @date 2020/4/9 22:04
     */
    @PostMapping
    public String addStuClass(StudentClass studentClass){
     // System.out.println("+>="+studentClass);
        int insert = studentClassMapper.insert(studentClass);
         if(insert>0){
             return "success";
         }
        return "false";
    }
    /*
     * @Description 根据前台传来的值，对当前班级的系别以及专业等进行修改
     * @param s 学生班级内的全部信息
     * @return java.lang.String
     * @author liusupeng
     * @date 2020/4/10 7:45
     */
    @PutMapping
    public String exitStuClass(StudentClass s){
        if(s.getStuClassIstId()==null){
            s.setStuClassIstId(0);
        }
        if(s.getStuClassTeaId()==null){
            s.setStuClassTeaId(0);
        }
        try {
            int i = studentClassMapper.updateById(s);
            if(i>0){
                return "success";
            }else{
                return "false";
            }
        }catch (Exception e){
            return "sqlfalse";
        }


    }
    /*
     * @Description 对班级进行删除，如果当前班级下有学生，会让他输入一个密码来确定是否删除，如果密码输入成功则将班级下的学生一并删除
        当然，删除了学生也要删除他的用户以及用户角色
     * @param stuClassId
     * @param pwd
     * @return java.lang.String
     * @author liusupeng
     * @date 2020/4/10 7:52
     */
    @DeleteMapping
    public String delStuClass(Integer stuClassId,String pwd){
QueryWrapper<Student> queryWrapper=new QueryWrapper<Student>();
queryWrapper.eq("stuClassId",stuClassId);
        if(pwd.equals("admin")){
            List<Student> students = studentMapper.selectList(queryWrapper);
            for(int i=0;i<students.size();i++){
                usersMapper.deleteById(students.get(i).getExit1());//删除用户
                userRolesMapper.delUsersRole(students.get(i).getExit1());//删除用户角色
            }


            Integer integer = studentMapper.delStudentByStuClassId(stuClassId);//删除学生
                System.out.println(integer);
                int i = studentClassMapper.deleteById(stuClassId);//删除班级
                if(i>0){
                    return "success";
                }


        }
            return "false";

    }

}
